package com.local.coupon.controller;

import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.coupon.pojo.Coupon;
import com.local.coupon.pojo.CouponListRequestParam;
import com.local.coupon.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;


    /**
     * 查询数据列表，支持翻页和搜索
     * @param requestParam
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestBody CouponListRequestParam requestParam) {
        Integer page = requestParam.getPage();
        Integer limit = requestParam.getLimit();
        String sort = requestParam.getSort();
        String order = requestParam.getOrder();
        String couponName = requestParam.getCouponName();
        Integer couponType = requestParam.getCouponType(); //优惠券赠送类型，如果是0则通用券，用户领取；如果是1，则是注册赠券；如果是2，则是优惠券码兑换；
        Integer couponStatus = requestParam.getCouponStatus(); //优惠券状态，如果是0则是正常可用；如果是1则是过期; 如果是2则是下架。
        String startTime = requestParam.getStartTime();
        String endTime = requestParam.getEndTime();
        Integer isDeleted = requestParam.getIsDeleted();
        page = Objects.isNull(page) || page <= 0 ? 1 : page;
        limit = Objects.isNull(limit) || limit <= 0 ? 1 : limit;
        sort = Objects.isNull(sort) || sort.isEmpty() ? "coupon_id" : sort;
        order = Objects.isNull(order) || order.isEmpty() ? "desc" : order;

        PageResultUtil<Coupon> couponPageResult = couponService.selectCouponList(page, limit, sort, order, couponName, couponType, couponStatus, startTime, endTime, isDeleted);
        if (Objects.nonNull(couponPageResult)) {
            return new ResultUtil(200, "", couponPageResult);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }


    /**
     * 新增
     * @param coupon
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody Coupon coupon) {
        Integer saveResult = couponService.save(coupon);
        if (saveResult > 0) {
            return new ResultUtil(200, "新增成功");
        } else {
            return new ResultUtil(201, "新增失败，请稍后再试");
        }
    }


    /**
     * 通过id查询数据
     * @param couponId
     * @return
     */
    @GetMapping("/{couponId}")
    @ResponseBody
    public ResultUtil getById(@PathVariable("couponId") Integer couponId) {
        Coupon coupon = couponService.selectById(couponId);
        if (Objects.nonNull(coupon)) {
            return new ResultUtil(200, "", coupon);
        } else {
            return new ResultUtil(201, "查询失败，请稍后再试");
        }
    }


    /**
     * 修改
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody Coupon coupon) {
        Integer updateResult = couponService.update(coupon);
        if (updateResult > 0) {
            return new ResultUtil(200, "修改成功");
        } else {
            return new ResultUtil(201, "修改失败，请稍后再试");
        }
    }


    /**
     * 根据id逻辑删除
     * @param couponId
     * @return
     */
    @DeleteMapping("/{couponId}")
    @ResponseBody
    public ResultUtil delete(@PathVariable("couponId") Integer couponId) {
        Integer deleteResult = couponService.delete(couponId);
        if (deleteResult > 0) {
            return new ResultUtil(200, "删除成功");
        } else {
            return new ResultUtil(201, "删除失败，请稍后再试");
        }
    }
}
